#!/bin/bash
export PGPASSWORD='Root123!'

# 读取主机列表
host=$(cat /root/host)

# 遍历主机列表
for j in $host
do
    # 查询 itemid
    jk_itemid=$(psql -U zabbix -h localhost -d zabbix -t -A -c "SELECT itemid FROM items WHERE hostid IN (SELECT hostid FROM hosts WHERE host = '$j') AND (name = 'cpu1分钟内负载的平均值' OR name = '检测agent服务是否正常');")

    # 遍历查询结果中的 itemid
    for i in $jk_itemid
    do
        # 查询历史记录并将结果追加到日志文件
        psql -U zabbix -h localhost -d zabbix -t -A -F $'\t' -c "SELECT itemid, to_timestamp(clock) + interval '8 hours', value FROM history WHERE itemid = $i ORDER BY clock DESC LIMIT 100;" >> /root/itemid/"$j".log
    done
done

